package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ApplyInfoMapper;

import static com.ruoyi.common.utils.DateUtils.getTime;
import com.ruoyi.common.utils.StringUtils;

/**
 * 制作申请Service业务层处理
 *
 * @author ess
 * @date 2021-10-25
 */
@Service
public class ApplyInfoServiceImpl implements IApplyInfoService
{
    @Autowired
    private ApplyInfoMapper applyInfoMapper;
    @Autowired
    private JmService jmService;

    /**
     * 查询制作申请
     *
     * @param applyInfoId 制作申请主键
     * @return 制作申请
     */
    @Override
    public ApplyInfo selectApplyInfoByApplyInfoId(String applyInfoId)
    {

        return getDApplyInfo(applyInfoMapper.selectApplyInfoByApplyInfoId(applyInfoId));
    }

    /**
     * 查询制作申请列表
     *
     * @param applyInfo 制作申请
     * @return 制作申请
     */
    @Override
    public List<ApplyInfo> selectApplyInfoList(ApplyInfo applyInfo)
    {
        List<ApplyInfo> applyInfoList= applyInfoMapper.selectApplyInfoList(getEApplyInfo(applyInfo));
        if (applyInfoList != null && applyInfoList.size() > 0) {
            for (ApplyInfo a:applyInfoList) {
                getDApplyInfo(a);
            }
            return applyInfoList;
        }
        return applyInfoList;
    }

    /**
     * 新增制作申请
     *
     * @param applyInfo 制作申请
     * @return 结果
     */
    @Override
    public int insertApplyInfo(ApplyInfo applyInfo)
    {
        //根据当前用户组织数据
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //组件ID
        applyInfo.setApplyInfoId(IdUtils.simpleUUID());
        //申请人的用户ID
        applyInfo.setApplyUserId(user.getUserId().toString());
        //单位ID
//        applyInfo.setUnitId(user.getDeptId().toString());
        //设置申请信息状态
        applyInfo.setApplyState(0L);
        //设置申请时间
        applyInfo.setApplyTime(getTime());

        applyInfo.setSealDetailId(IdUtils.simpleUUID());
        return applyInfoMapper.insertApplyInfo(getEApplyInfo(applyInfo));
    }

    /**
     * 修改制作申请
     *
     * @param applyInfo 制作申请
     * @return 结果
     */
    @Override
    public int updateApplyInfo(ApplyInfo applyInfo)
    {
        return applyInfoMapper.updateApplyInfo(getEApplyInfo(applyInfo));
    }

    /**
     * 批量删除制作申请
     *
     * @param applyInfoIds 需要删除的制作申请主键
     * @return 结果
     */
    @Override
    public int deleteApplyInfoByApplyInfoIds(String[] applyInfoIds)
    {
        return applyInfoMapper.deleteApplyInfoByApplyInfoIds(applyInfoIds);
    }

    /**
     * 删除制作申请信息
     *
     * @param applyInfoId 制作申请主键
     * @return 结果
     */
    @Override
    public int deleteApplyInfoByApplyInfoId(String applyInfoId)
    {
        return applyInfoMapper.deleteApplyInfoByApplyInfoId(applyInfoId);
    }

    /**
     * 修改制作记录的状态
     *
     * @param applyInfoId 记录ID
     * @param ls 状态码
     * @return 结果
     */
    @Override
    public int updateApplyState(String applyInfoId, long ls) {
        ApplyInfo applyInfo= selectApplyInfoByApplyInfoId(applyInfoId);
        applyInfo.setApplyState(ls);
        return updateApplyInfo(getEApplyInfo(applyInfo));
    }

    @Override
    public ApplyInfo selectApplyInfoBySealDetailId(String sealDetailId) {
        return getDApplyInfo(applyInfoMapper.selectApplyInfoBySealDetailId(sealDetailId));
    }

    //加密
    private ApplyInfo getEApplyInfo(ApplyInfo applyInfo) {
        if (applyInfo != null) {
            if (applyInfo.getSealName() != null) {
                applyInfo.setSealName(jmService.getEncryptString(applyInfo.getSealName()));
            }

        }

        return applyInfo;
    }

    //    解密
    private ApplyInfo getDApplyInfo(ApplyInfo applyInfo) {
        if (applyInfo != null) {
            if (applyInfo.getSealName() != null) {
                applyInfo.setSealName(jmService.getDecryptString(applyInfo.getSealName()));
            }
            if (applyInfo.getApplyUserName() != null) {
                applyInfo.setApplyUserName(jmService.getDecryptString(applyInfo.getApplyUserName()));
            }
            if (applyInfo.getReviewUserName() != null) {
                applyInfo.setReviewUserName(jmService.getDecryptString(applyInfo.getReviewUserName()));
            }
            if (applyInfo.getMakeUserName() != null) {
                applyInfo.setMakeUserName(jmService.getDecryptString(applyInfo.getMakeUserName()));
            }

        }
        return applyInfo;
    }

}
